﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.IO;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Linq.Expressions;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Web;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BaoJianSoft.Common;
using BaoJianSoft.Common.Caching;
using BaoJianSoft.Common.Web;
using BaoJianSoft.Platform.Linq;

namespace BaoJianSoft.Platform.Services
{
	/// <summary>
	/// The service implementation to search roles
	/// </summary>
	[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
	public class RoleService : IRoleService
	{
		private static ICache cache = SpringContext.Current.GetObject<ICache>();
		private static IOrganizationApi organizationApi = SpringContext.Current.GetObject<IOrganizationApi>();
		private static IRoleApi roleApi = SpringContext.Current.GetObject<IRoleApi>();

		/// <summary>
		/// Get all roles associated to the specified organization.
		/// </summary>
		/// <param name="organizationId"></param>
		/// <returns></returns>
		public Collection<RoleObject> FindByOrganizationIdJson(string organizationId)
		{
			if (string.IsNullOrEmpty(organizationId))
				throw new BadRequestException("The parameter \"organizationId\" is not specified.");

			Guid organizationIdValue = Guid.Empty;
			try
			{
				organizationIdValue = new Guid(organizationId);
			}
			catch
			{
				throw new BadRequestException("The parameter \"organizationId\" is invalid.");
			}

			OrganizationObject organizationObject = organizationApi.GetOrganization(organizationIdValue);
			if (organizationObject == null)
				throw new BadRequestException("The parameter \"organizationId\" is invalid.");

			int recordCount;
			Expression<Func<Role, bool>> predicate = role => role.RolesInOrganizationTypes.Any(rolesInOrganizationType => rolesInOrganizationType.OrganizationTypeId == organizationObject.OrganizationTypeId);
			IEnumerable<RoleObject> results = roleApi.FindRoles(predicate, null, "RoleName ASC", 0, int.MaxValue, out recordCount);
			return new Collection<RoleObject>(results.ToList());
		}
	}
}
